﻿Public Class Categories
    Inherits System.Web.UI.Page

    Public DBManager As DBManager = New DBManager

    'To set masterpage 
    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        'Check if user is null
        If (Session("user") IsNot Nothing) Then
            'Retrieve userAcc details from session
            Dim userLoggedIn As userAcc = Session("user")
            'Check if user is admin, assign masterpage respectively
            If userLoggedIn.isAdmin Then
                Me.MasterPageFile = "~/MasterPage/Admin.Master"
            Else
                Me.MasterPageFile = "~/MasterPage/User.Master"
            End If
        Else
            'For anonymous users
            Me.MasterPageFile = "~/MasterPage/User.Master"
            'Enable this line to prevent anonymous users from entering this page
            ' Response.Redirect("~/Login.aspx")
        End If
    End Sub




    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Try
                If (Request.QueryString("id") IsNot Nothing) Then
                    Dim fullID = Request.QueryString("id")

                    Dim type = fullID(0)

                    Select Case type
                        Case "r"
                            Dim splitIDString = fullID.Split("r")
                            lblCategoryName.Text = "Retail"
                            LblCategoryName1.Text = "Retail"
                            ' getting the top 5 business
                            Dim imageArray As ArrayList = DBManager.getTop5PopularByCategory("Retail")
                            'Set image url
                            imageFront1.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(0)
                            imageFront2.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(1)
                            imageFront3.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(2)
                            imageFront4.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(3)
                            imageFront5.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(4)

                            'Get outlet id 
                            Dim outletID1 = DBManager.getOutletImageID(imageArray(0))
                            Dim outletID2 = DBManager.getOutletImageID(imageArray(1))
                            Dim outletID3 = DBManager.getOutletImageID(imageArray(2))
                            Dim outletID4 = DBManager.getOutletImageID(imageArray(3))
                            Dim outletID5 = DBManager.getOutletImageID(imageArray(4))

                            'Set postback url
                            imageFront1.PostBackUrl = "OutletReview.aspx?id=" + outletID1.ToString()
                            imageFront2.PostBackUrl = "OutletReview.aspx?id=" + outletID2.ToString()
                            imageFront3.PostBackUrl = "OutletReview.aspx?id=" + outletID3.ToString()
                            imageFront4.PostBackUrl = "OutletReview.aspx?id=" + outletID4.ToString()
                            imageFront5.PostBackUrl = "OutletReview.aspx?id=" + outletID5.ToString()

                            ' set the text for the hyperlink 
                            hlCompanyNameT1.Text = DBManager.getBusinessOutletName(outletID1)
                            hlCompanyNameT2.Text = DBManager.getBusinessOutletName(outletID2)
                            hlCompanyNameT3.Text = DBManager.getBusinessOutletName(outletID3)
                            hlCompanyNameT4.Text = DBManager.getBusinessOutletName(outletID4)
                            hlCompanyNameT5.Text = DBManager.getBusinessOutletName(outletID5)

                            'set the link for the hyperlink
                            hlCompanyNameT1.NavigateUrl = "OutletReview.aspx?id=" + outletID1.ToString()
                            hlCompanyNameT2.NavigateUrl = "OutletReview.aspx?id=" + outletID2.ToString()
                            hlCompanyNameT3.NavigateUrl = "OutletReview.aspx?id=" + outletID3.ToString()
                            hlCompanyNameT4.NavigateUrl = "OutletReview.aspx?id=" + outletID4.ToString()
                            hlCompanyNameT5.NavigateUrl = "OutletReview.aspx?id=" + outletID5.ToString()


                            Dim SubCategory() As String = {"Fashion & Clothing ", "Beauty & Sports", "Home & Electronics", "Books & Music"}
                            'if it is retail
                            If (lblCategoryName.Text = "Retail" And LblCategoryName1.Text = "Retail") Then
                                'count the number of sub category
                                Dim numOfSubCategory = SubCategory.Count
                                Dim counter = 1
                                Dim LastAddSubCategory = numOfSubCategory + 1
                                For Each categoryItem As String In SubCategory
                                    If (Not counter = LastAddSubCategory) Then
                                        Dim SubCategoryName = "SubCategoryLabel" + counter.ToString()
                                        Dim hfSubCategoryName = "hfSubCategory" + counter.ToString()
                                        Dim subCategoryTableName = "SubCategoryTable" + counter.ToString()
                                        Dim listviewName = "ListView" + counter.ToString()
                                        Dim name As Label = SubCategoryLabel1.Parent.FindControl(SubCategoryName)
                                        name.Text = categoryItem
                                        Dim hf As HiddenField = hfSubCategory1.Parent.FindControl(hfSubCategoryName)
                                        hf.Value = categoryItem
                                        Dim SubCategoryListView As ListView = ListView1.Parent.FindControl(listviewName)
                                        SubCategoryListView.Visible = True
                                        name.Visible = True
                                        counter = counter + 1
                                    End If
                                Next

                                If (lblCategoryName.Text = "Retail" And LblCategoryName1.Text = "Retail") Then
                                    Panel1.Visible = False
                                    Panel2.Visible = False
                                End If

                            End If
                        Case "e"
                            Dim splitIDString = fullID.Split("e")
                            lblCategoryName.Text = "Entertainment"
                            LblCategoryName1.Text = "Entertainment"

                            Dim imageArray As ArrayList = DBManager.getTop5PopularByCategory("Entertainment")
                            'Set image url
                            imageFront1.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(0)
                            imageFront2.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(1)
                            imageFront3.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(2)
                            imageFront4.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(3)
                            imageFront5.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(4)

                            'Get outlet url to redirect to 
                            Dim outletID1 = DBManager.getOutletImageID(imageArray(0))
                            Dim outletID2 = DBManager.getOutletImageID(imageArray(1))
                            Dim outletID3 = DBManager.getOutletImageID(imageArray(2))
                            Dim outletID4 = DBManager.getOutletImageID(imageArray(3))
                            Dim outletID5 = DBManager.getOutletImageID(imageArray(4))

                            'Set postback url
                            imageFront1.PostBackUrl = "OutletReview.aspx?id=" + outletID1.ToString()
                            imageFront2.PostBackUrl = "OutletReview.aspx?id=" + outletID2.ToString()
                            imageFront3.PostBackUrl = "OutletReview.aspx?id=" + outletID3.ToString()
                            imageFront4.PostBackUrl = "OutletReview.aspx?id=" + outletID4.ToString()
                            imageFront5.PostBackUrl = "OutletReview.aspx?id=" + outletID5.ToString()
                            ' set the text for the hyperlink 
                            hlCompanyNameT1.Text = DBManager.getBusinessOutletName(outletID1)
                            hlCompanyNameT2.Text = DBManager.getBusinessOutletName(outletID2)
                            hlCompanyNameT3.Text = DBManager.getBusinessOutletName(outletID3)
                            hlCompanyNameT4.Text = DBManager.getBusinessOutletName(outletID4)
                            hlCompanyNameT5.Text = DBManager.getBusinessOutletName(outletID5)
                            'set the link for the hyperlink
                            hlCompanyNameT1.NavigateUrl = "OutletReview.aspx?id=" + outletID1.ToString()
                            hlCompanyNameT2.NavigateUrl = "OutletReview.aspx?id=" + outletID2.ToString()
                            hlCompanyNameT3.NavigateUrl = "OutletReview.aspx?id=" + outletID3.ToString()
                            hlCompanyNameT4.NavigateUrl = "OutletReview.aspx?id=" + outletID4.ToString()
                            hlCompanyNameT5.NavigateUrl = "OutletReview.aspx?id=" + outletID5.ToString()


                            Dim SubCategory() As String = {"Night Clubs", "Gaming", "Cinema", "Arts", "Museums"}
                            'if it is retail
                            If (lblCategoryName.Text = "Entertainment" And LblCategoryName1.Text = "Entertainment") Then
                                'count the number of sub category
                                Dim numOfSubCategory = SubCategory.Count
                                Dim counter = 1
                                Dim LastAddSubCategory = numOfSubCategory + 1
                                For Each categoryItem As String In SubCategory
                                    If (Not counter = LastAddSubCategory) Then
                                        Dim SubCategoryName = "SubCategoryLabel" + counter.ToString()
                                        Dim hfSubCategoryName = "hfSubCategory" + counter.ToString()
                                        Dim listviewName = "ListView" + counter.ToString()
                                        Dim name As Label = SubCategoryLabel1.Parent.FindControl(SubCategoryName)
                                        name.Text = categoryItem
                                        Dim hf As HiddenField = hfSubCategory1.Parent.FindControl(hfSubCategoryName)
                                        hf.Value = categoryItem
                                        Dim SubCategoryListView As ListView = ListView1.Parent.FindControl(listviewName)
                                        SubCategoryListView.Visible = True
                                        name.Visible = True
                                        counter = counter + 1

                                    Else
                                        MsgBox("You have successfully deleted this branch from your followed branch list", MsgBoxStyle.OkOnly, "Successfully deleted this branch from your followed branch list")
                                    End If

                                Next

                            End If

                            If (lblCategoryName.Text = "Entertainment" And LblCategoryName1.Text = "Entertainment") Then
                                Panel1.Visible = False
                            End If

                        Case "u"
                            Dim splitIDString = fullID.Split("u")
                            lblCategoryName.Text = "Education"
                            LblCategoryName1.Text = "Education"

                            Dim imageArray As ArrayList = DBManager.getTop5PopularByCategory("Education")

                            'Set image url
                            imageFront1.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(0)
                            imageFront2.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(1)
                            imageFront3.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(2)
                            imageFront4.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(3)
                            imageFront5.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(4)

                            'Get outlet url to redirect to 
                            Dim outletID1 = DBManager.getOutletImageID(imageArray(0))
                            Dim outletID2 = DBManager.getOutletImageID(imageArray(1))
                            Dim outletID3 = DBManager.getOutletImageID(imageArray(2))
                            Dim outletID4 = DBManager.getOutletImageID(imageArray(3))
                            Dim outletID5 = DBManager.getOutletImageID(imageArray(4))

                            'Set postback url
                            imageFront1.PostBackUrl = "OutletReview.aspx?id=" + outletID1.ToString()
                            imageFront2.PostBackUrl = "OutletReview.aspx?id=" + outletID2.ToString()
                            imageFront3.PostBackUrl = "OutletReview.aspx?id=" + outletID3.ToString()
                            imageFront4.PostBackUrl = "OutletReview.aspx?id=" + outletID4.ToString()
                            imageFront5.PostBackUrl = "OutletReview.aspx?id=" + outletID5.ToString()
                            ' set the text for the hyperlink 
                            hlCompanyNameT1.Text = DBManager.getBusinessOutletName(outletID1)
                            hlCompanyNameT2.Text = DBManager.getBusinessOutletName(outletID2)
                            hlCompanyNameT3.Text = DBManager.getBusinessOutletName(outletID3)
                            hlCompanyNameT4.Text = DBManager.getBusinessOutletName(outletID4)
                            hlCompanyNameT5.Text = DBManager.getBusinessOutletName(outletID5)
                            'set the link for the hyperlink
                            hlCompanyNameT1.NavigateUrl = "OutletReview.aspx?id=" + outletID1.ToString()
                            hlCompanyNameT2.NavigateUrl = "OutletReview.aspx?id=" + outletID2.ToString()
                            hlCompanyNameT3.NavigateUrl = "OutletReview.aspx?id=" + outletID3.ToString()
                            hlCompanyNameT4.NavigateUrl = "OutletReview.aspx?id=" + outletID4.ToString()
                            hlCompanyNameT5.NavigateUrl = "OutletReview.aspx?id=" + outletID5.ToString()


                            Dim SubCategory() As String = {"Tuition Centre", "Enrichment Classes"}
                            'if it is education
                            If (lblCategoryName.Text = "Education" And LblCategoryName1.Text = "Education") Then
                                'count the number of sub category
                                Dim numOfSubCategory = SubCategory.Count
                                Dim counter = 1
                                Dim LastAddSubCategory = numOfSubCategory + 1
                                For Each categoryItem As String In SubCategory
                                    If (Not counter = LastAddSubCategory) Then
                                        Dim SubCategoryName = "SubCategoryLabel" + counter.ToString()
                                        Dim hfSubCategoryName = "hfSubCategory" + counter.ToString()
                                        Dim listviewName = "ListView" + counter.ToString()
                                        Dim name As Label = SubCategoryLabel1.Parent.FindControl(SubCategoryName)
                                        name.Text = categoryItem
                                        Dim hf As HiddenField = hfSubCategory1.Parent.FindControl(hfSubCategoryName)
                                        hf.Value = categoryItem
                                        Dim SubCategoryListView As ListView = ListView1.Parent.FindControl(listviewName)
                                        SubCategoryListView.Visible = True
                                        name.Visible = True
                                        counter = counter + 1


                                    End If
                                Next
                            End If


                            If (lblCategoryName.Text = "Education" And LblCategoryName1.Text = "Education") Then
                                Panel1.Visible = False
                                Panel2.Visible = False
                                Panel3.Visible = False
                                Panel3.Visible = False
                            End If

                        Case "h"
                            Dim splitIDString = fullID.Split("h")


                            lblCategoryName.Text = "Health & Beauty"
                            LblCategoryName1.Text = "Health & Beauty"

                            Dim imageArray As ArrayList = DBManager.getTop5PopularByCategory("Health & Beauty")
                            'Set image url
                            imageFront1.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(0)
                            imageFront2.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(1)
                            imageFront3.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(2)
                            imageFront4.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(3)
                            imageFront5.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(4)

                            'Get outlet url to redirect to 
                            Dim outletID1 = DBManager.getOutletImageID(imageArray(0))
                            Dim outletID2 = DBManager.getOutletImageID(imageArray(1))
                            Dim outletID3 = DBManager.getOutletImageID(imageArray(2))
                            Dim outletID4 = DBManager.getOutletImageID(imageArray(3))
                            Dim outletID5 = DBManager.getOutletImageID(imageArray(4))

                            'Set postback url
                            imageFront1.PostBackUrl = "OutletReview.aspx?id=" + outletID1.ToString()
                            imageFront2.PostBackUrl = "OutletReview.aspx?id=" + outletID2.ToString()
                            imageFront3.PostBackUrl = "OutletReview.aspx?id=" + outletID3.ToString()
                            imageFront4.PostBackUrl = "OutletReview.aspx?id=" + outletID4.ToString()
                            imageFront5.PostBackUrl = "OutletReview.aspx?id=" + outletID5.ToString()
                            ' set the text for the hyperlink 
                            hlCompanyNameT1.Text = DBManager.getBusinessOutletName(outletID1)
                            hlCompanyNameT2.Text = DBManager.getBusinessOutletName(outletID2)
                            hlCompanyNameT3.Text = DBManager.getBusinessOutletName(outletID3)
                            hlCompanyNameT4.Text = DBManager.getBusinessOutletName(outletID4)
                            hlCompanyNameT5.Text = DBManager.getBusinessOutletName(outletID5)
                            'set the link for the hyperlink
                            hlCompanyNameT1.NavigateUrl = "OutletReview.aspx?id=" + outletID1.ToString()
                            hlCompanyNameT2.NavigateUrl = "OutletReview.aspx?id=" + outletID2.ToString()
                            hlCompanyNameT3.NavigateUrl = "OutletReview.aspx?id=" + outletID3.ToString()
                            hlCompanyNameT4.NavigateUrl = "OutletReview.aspx?id=" + outletID4.ToString()
                            hlCompanyNameT5.NavigateUrl = "OutletReview.aspx?id=" + outletID5.ToString()



                            Dim SubCategory() As String = {"Doctors", "Dentists", "Clinics", "Medical Spas/Spas", "Makeup Artist"}
                            'if it is education
                            If (lblCategoryName.Text = "Health & Beauty" And LblCategoryName1.Text = "Health & Beauty") Then
                                'count the number of sub category
                                Dim numOfSubCategory = SubCategory.Count
                                Dim counter = 1
                                Dim LastAddSubCategory = numOfSubCategory + 1
                                For Each categoryItem As String In SubCategory
                                    If (Not counter = LastAddSubCategory) Then
                                        Dim SubCategoryName = "SubCategoryLabel" + counter.ToString()
                                        Dim hfSubCategoryName = "hfSubCategory" + counter.ToString()
                                        Dim listviewName = "ListView" + counter.ToString()
                                        Dim name As Label = SubCategoryLabel1.Parent.FindControl(SubCategoryName)
                                        name.Text = categoryItem
                                        Dim hf As HiddenField = hfSubCategory1.Parent.FindControl(hfSubCategoryName)
                                        hf.Value = categoryItem
                                        Dim SubCategoryListView As ListView = ListView1.Parent.FindControl(listviewName)
                                        SubCategoryListView.Visible = True
                                        name.Visible = True
                                        counter = counter + 1

                                   
                                    End If
                                Next
                            End If


                            If (lblCategoryName.Text = "Health & Beauty" And LblCategoryName1.Text = "Health & Beauty") Then
                                Panel1.Visible = False
                            End If

                        Case "p"
                            Dim splitIDString = fullID.Split("p")
                            lblCategoryName.Text = "Pets"
                            LblCategoryName1.Text = "Pets"

                            Dim imageArray As ArrayList = DBManager.getTop5PopularByCategory("Pets")
                            'Set image url
                            imageFront1.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(0)
                            imageFront2.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(1)
                            imageFront3.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(2)
                            imageFront4.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(3)
                            imageFront5.ImageUrl = "IHttpHandler.ashx?id=" + imageArray(4)

                            'Get outlet url to redirect to 
                            Dim outletID1 = DBManager.getOutletImageID(imageArray(0))
                            Dim outletID2 = DBManager.getOutletImageID(imageArray(1))
                            Dim outletID3 = DBManager.getOutletImageID(imageArray(2))
                            Dim outletID4 = DBManager.getOutletImageID(imageArray(3))
                            Dim outletID5 = DBManager.getOutletImageID(imageArray(4))

                            'Set postback url
                            imageFront1.PostBackUrl = "OutletReview.aspx?id=" + outletID1.ToString()
                            imageFront2.PostBackUrl = "OutletReview.aspx?id=" + outletID2.ToString()
                            imageFront3.PostBackUrl = "OutletReview.aspx?id=" + outletID3.ToString()
                            imageFront4.PostBackUrl = "OutletReview.aspx?id=" + outletID4.ToString()
                            imageFront5.PostBackUrl = "OutletReview.aspx?id=" + outletID5.ToString()
                            ' set the text for the hyperlink 
                            hlCompanyNameT1.Text = DBManager.getBusinessOutletName(outletID1)
                            hlCompanyNameT2.Text = DBManager.getBusinessOutletName(outletID2)
                            hlCompanyNameT3.Text = DBManager.getBusinessOutletName(outletID3)
                            hlCompanyNameT4.Text = DBManager.getBusinessOutletName(outletID4)
                            hlCompanyNameT5.Text = DBManager.getBusinessOutletName(outletID5)
                            'set the link for the hyperlink
                            hlCompanyNameT1.NavigateUrl = "OutletReview.aspx?id=" + outletID1.ToString()
                            hlCompanyNameT2.NavigateUrl = "OutletReview.aspx?id=" + outletID2.ToString()
                            hlCompanyNameT3.NavigateUrl = "OutletReview.aspx?id=" + outletID3.ToString()
                            hlCompanyNameT4.NavigateUrl = "OutletReview.aspx?id=" + outletID4.ToString()
                            hlCompanyNameT5.NavigateUrl = "OutletReview.aspx?id=" + outletID5.ToString()




                            Dim SubCategory() As String = {"Pet Accessories", "Veterinarians"}
                            'if it is education
                            If (lblCategoryName.Text = "Pets" And LblCategoryName1.Text = "Pets") Then
                                'count the number of sub category
                                Dim numOfSubCategory = SubCategory.Count
                                Dim counter = 1
                                Dim LastAddSubCategory = numOfSubCategory + 1
                                For Each categoryItem As String In SubCategory
                                    If (Not counter = LastAddSubCategory) Then
                                        Dim SubCategoryName = "SubCategoryLabel" + counter.ToString()
                                        Dim hfSubCategoryName = "hfSubCategory" + counter.ToString()
                                        Dim listviewName = "ListView" + counter.ToString()
                                        Dim name As Label = SubCategoryLabel1.Parent.FindControl(SubCategoryName)
                                        name.Text = categoryItem
                                        Dim hf As HiddenField = hfSubCategory1.Parent.FindControl(hfSubCategoryName)
                                        hf.Value = categoryItem
                                        Dim SubCategoryListView As ListView = ListView1.Parent.FindControl(listviewName)
                                        SubCategoryListView.Visible = True
                                        name.Visible = True
                                        counter = counter + 1
                                    End If
                                Next
                            End If

                            If (lblCategoryName.Text = "Health & Beauty" And LblCategoryName1.Text = "Health & Beauty") Then
                                Panel1.Visible = False
                                Panel2.Visible = False
                                Panel3.Visible = False
                                Panel3.Visible = False
                            End If

                    End Select

                Else
                    Response.Redirect("home.aspx")
                End If
            Catch ex As Exception
            End Try
        End If
    End Sub

    Protected Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ListView1.SelectedIndexChanged

    End Sub
End Class